package com.sk.sourcecircle.dialog;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sk.sourcecircle.R;
import com.sk.sourcecircle.callback.MyCallBack;
import com.sk.sourcecircle.entity.EventDetailsBean;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.TicketsRadioButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/11/19.
 */

public class BuyNextDialogFragment extends DialogFragment {

    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.ticket_ll)
    LinearLayout ticketLl;
    @BindView(R.id.reduce)
    TextView reduce;
    @BindView(R.id.buy_num)
    TextView buyNum;
    @BindView(R.id.add)
    TextView add;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.next)
    TextView next;
    Unbinder unbinder;
    private MyCallBack myCallBack;
    private double total = 0;
    private int num;

    List<TicketsRadioButton> ticketsRadioButtons = new ArrayList<>();

    private TicketsRadioButton checkedButton;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    private List<EventDetailsBean.DataBeanX.DataBean> dataBeanList = new ArrayList<>();

    public List<EventDetailsBean.DataBeanX.DataBean> getDataBeanList() {
        return dataBeanList;
    }

    public void setDataBeanList(List<EventDetailsBean.DataBeanX.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = android.support.v4.app.DialogFragment.STYLE_NO_TITLE, theme = 0;
        setStyle(style, theme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bug_next_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        for (EventDetailsBean.DataBeanX.DataBean dataBean : dataBeanList) {
            LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.item_ticket_radio, null);
            final TicketsRadioButton ticketsRadioButton = linearLayout.findViewById(R.id.radio);
            ticketsRadioButton.setText(dataBean.getName());
            ticketsRadioButton.setTicketMoney("¥" + dataBean.getPrice());
            ticketsRadioButton.setTag(dataBean);
            ticketsRadioButtons.add(ticketsRadioButton);
            ticketsRadioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ticketsRadioButton.isChecked()) {
                        checkedButton = ticketsRadioButton;
                        calculateTotal();
                        for (TicketsRadioButton button : ticketsRadioButtons) {
                            if (ticketsRadioButton != button) {
                                button.setChecked(false);
                            }
                        }
                    }
                }
            });
            ticketLl.addView(linearLayout);
        }
    }


    public void onStart() {
        super.onStart();
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.close, R.id.reduce, R.id.add, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close:
                dismiss();
                break;
            case R.id.reduce:

                if (checkedButton != null) {
                    int number = Integer.parseInt(buyNum.getText().toString());
                    number--;
                    if (number <= 1) {
                        buyNum.setText(String.valueOf(1));
                    } else {
                        buyNum.setText(String.valueOf(number));
                    }
                    calculateTotal();
                } else {
                    Toast.makeText(getActivity(), "请选择您需要的票种!", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.add:
                if (checkedButton != null) {
                    int num = Integer.parseInt(buyNum.getText().toString());
                    num++;
                    buyNum.setText(String.valueOf(num));
                    calculateTotal();
                } else {
                    Toast.makeText(getActivity(), "请选择您需要的票种!", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.next:
                myCallBack.sure();
                dismiss();
                break;
        }
    }

    public TicketsRadioButton getCheckedButton() {
        return checkedButton;
    }

    private void calculateTotal() {
        if (checkedButton != null) {
            EventDetailsBean.DataBeanX.DataBean dataBean = (EventDetailsBean.DataBeanX.DataBean) checkedButton.getTag();
            double money = Double.parseDouble(dataBean.getPrice());
            double total = MyUtils.mul(money, Double.parseDouble(buyNum.getText().toString()));
            tvTotal.setText("合计：" + total + "元");
            this.total = total;
        }
    }

    public double getTotal() {
        return total;
    }

    public int getNum() {
        return Integer.parseInt(buyNum.getText().toString());
    }

}
